package com.jrh.biz.feature;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jrh.common.constant.JrhBizConstant;
import com.jrh.dal.FeatureDao;
import com.jrh.dal.FeatureValueDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Feature;
import com.jrh.domain.entity.FeatureValue;
import com.jrh.domain.entity.Product;

public class FeatureServiceImpl implements FeatureService {

    private FeatureDao      featureDao;
    private FeatureValueDao featureValueDao;

    public void setFeatureValueDao(FeatureValueDao featureValueDao) {
        this.featureValueDao = featureValueDao;
    }

    public void setFeatureDao(FeatureDao featureDao) {
        this.featureDao = featureDao;
    }

    @Override
    public Page<Feature> listFeatureByCondition(String categoryId, Integer start, Integer pageSize) {
        List<Feature> featureList = featureDao.listFeatureByCondition(categoryId, (start - 1) * pageSize, pageSize);
        int featureCount = featureDao.countFeatureByCondition(categoryId);
        Page<Feature> page = new Page<Feature>(featureList, (start - 1) * pageSize, pageSize, featureCount);
        return page;
    }

    @Override
    public void createFeature(Feature feature) {
        featureDao.create(feature);
        if (StringUtils.isEmpty(feature.getValues())) {
            return;
        }
        String values = feature.getValues();
        String[] valueArray = values.split(SPLIT_STRING);
        for (int i = 0; i < valueArray.length; i++) {
            FeatureValue fv = new FeatureValue();
            fv.setName(valueArray[i]);
            fv.setSort(i + 1);
            fv.setFeatureId(feature.getId());
            featureValueDao.create(fv);
        }
    }

    @Override
    public void deleteFeature(long id) {
        featureDao.delete(id);
        featureValueDao.deleteFeatureValueByFeatureId(id);
    }

    @Override
    public void updateFeature(Feature feature) {
        featureDao.update(feature);
        featureValueDao.deleteFeatureValueByFeatureId(feature.getId());
        if (StringUtils.isEmpty(feature.getValues())) {
            return;
        }
        String values = feature.getValues();
        String[] valueArray = values.split(SPLIT_STRING);
        for (int i = 0; i < valueArray.length; i++) {
            FeatureValue fv = new FeatureValue();
            fv.setName(valueArray[i]);
            fv.setSort(i + 1);
            fv.setFeatureId(feature.getId());
            featureValueDao.create(fv);
        }
    }

    @Override
    public Feature getFeatureById(long id) {
        Feature feature = featureDao.load(id);
        List<FeatureValue> featureValueList = featureValueDao.listFeatureValueByFeatureId(id);
        if (featureValueList == null) {
            return feature;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < featureValueList.size(); i++) {
            FeatureValue fv = featureValueList.get(i);
            if (fv == null) {
                continue;
            }
            if (i != 0) {
                sb.append(SPLIT_STRING_REAL);
            }
            sb.append(fv.getName());
        }
        feature.setValues(sb.toString());
        return feature;
    }

    @Override
    public List<Feature> listFeatureDetailByCategory(String categoryId) {
        List<Feature> featureList = featureDao.listFeatureDetailByCategory(categoryId);
        if (featureList == null || featureList.isEmpty()) {
            return null;
        }
        List<Feature> removeFeatures = new ArrayList<Feature>();
        List<FeatureValue> featureValueList = null;
        long lastFeatureId = -1;
        for (int i = 0; i < featureList.size(); i++) {
            Feature feature = featureList.get(i);
            FeatureValue fv = new FeatureValue();
            fv.setName(feature.getVn());
            fv.setId(feature.getVi());
            if (feature.getId() != lastFeatureId) {
                featureValueList = new ArrayList<FeatureValue>();
                feature.setFvList(featureValueList);
            } else {
                removeFeatures.add(feature);
            }
            featureValueList.add(fv);
            lastFeatureId = feature.getId();
        }
        featureList.removeAll(removeFeatures);
        return featureList;
    }

    @Override
    public List<Feature> listFeatureDetailByProduct(Long pId) {
        List<Feature> featureList = featureDao.listFeatureDetailByProduct(pId);
        if (featureList == null || featureList.isEmpty()) {
            return null;
        }
        List<Feature> removeFeatures = new ArrayList<Feature>();
        List<FeatureValue> featureValueList = null;
        long lastFeatureId = -1;
        for (int i = 0; i < featureList.size(); i++) {
            Feature feature = featureList.get(i);
            FeatureValue fv = new FeatureValue();
            fv.setName(feature.getVn());
            fv.setId(feature.getVi());
            if (feature.getId() != lastFeatureId) {
                featureValueList = new ArrayList<FeatureValue>();
                feature.setFvList(featureValueList);
            } else {
                removeFeatures.add(feature);
            }
            featureValueList.add(fv);
            lastFeatureId = feature.getId();
        }
        featureList.removeAll(removeFeatures);
        return featureList;
    }

    public List<Feature> listFeatureDetailByCatAndProd(String categoryId, String productId) {
        List<Feature> fList = listFeatureDetailByCategory(categoryId);
        if (StringUtils.isBlank(productId) || fList == null || JrhBizConstant.NULL_VALUE.equalsIgnoreCase(productId)) {
            return fList;
        }
        List<Feature> featureList = featureDao.listFeatureDetailByProduct(Long.parseLong(productId));
        Map<Long, Boolean> params = new HashMap<Long, Boolean>();
        for (Feature feature : featureList) {
            params.put(feature.getVi(), true);
        }
        for (Feature feature : fList) {
            if (feature == null || feature.getFvList() == null) {
                continue;
            }
            for (FeatureValue fv : feature.getFvList()) {
                if (params.containsKey(fv.getId())) {
                    fv.setChecked(true);
                }
            }
        }
        return fList;
    }
}
